package com.ymatou.iapi.optrade.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.AuthenticateFeeTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.AuthenticateFreightTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.AuthenticateCenterNotifyReq;
import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymatou.iapi.optrade.parameter.vo.IdentifyProdArrivedVo;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.AuthenticateCenterNotifyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceAuthenticateCallService;
import com.ymttest.database.sharding.db.ext.model.DeliveryPo;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sharding.db.sqlwapper.OrderWapper;

public class Ts_AuthenticateCenterNotify {
	
	private static AuthenticateCenterNotifyReq authenticateCenterNotifyReq;
	private static AuthenticateCenterNotifyCall authenticateCenterNotifyCall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	private static OrderWapper orderWapper = new OrderWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("到达鉴别中心通知");
	}

	@Before
	public void caseUp() {

		authenticateCenterNotifyReq = new AuthenticateCenterNotifyReq();
		authenticateCenterNotifyCall = new AuthenticateCenterNotifyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(authenticateCenterNotifyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AuthenticateCenterNotify_001() {
		Logger.start(true, "到达鉴别中心成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticate(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			
			Order order = placeOrder.getOrderList().get(0);
			
			Long userId = placeOrder.getUserId();
			Long sellerId = placeOrder.getOrderList().get(0).getSellerId();
			Long orderId = placeOrder.getOrderList().get(0).getOrderId();
			
			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(), placeOrder.getMainOrderId());
			
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			
			String billNo = TradingCallService.delivery(sellerId, orderId, false);
			Thread.sleep(3000);
			
			Logger.comment("前置数据准备结束");

			String catalogId = order.getOrderDetailPoList().get(0)
					.getCatalogId();

			// 准备参数
			List<IdentifyProdArrivedVo> prodArriveds = new ArrayList<>();
			
			IdentifyProdArrivedVo identifyProdArrivedVo = new IdentifyProdArrivedVo();
			
			identifyProdArrivedVo.setCatalogId(catalogId);
			identifyProdArrivedVo.setLogisticNo(billNo);
			identifyProdArrivedVo.setArrivedNumber(1);
			
			prodArriveds.add(identifyProdArrivedVo);
			
			authenticateCenterNotifyReq.setProdArriveds(prodArriveds);
			authenticateCenterNotifyReq.setOrderId(orderId);

			authenticateCenterNotifyCall.setData(authenticateCenterNotifyReq);
			authenticateCenterNotifyCall.callService();
			
			Logger.verifyEquals("ok",
					authenticateCenterNotifyCall.getReturnData(), "验证返回值");

			Logger.comment("验证order_detail");
			
			OrderDetailPo orderDetailPo = orderShardingWapper.selectOrderDetailbyOrderIdAndCatalogId(orderId,
					catalogId);
			
			Logger.verifyEquals(true, orderDetailPo.getInAuthenticateCenter(),
					"验证inAuthenticateCenter");
			Logger.verifyEquals(true, Utils.calcTimeSpanMin(orderDetailPo
					.getInAuthenticateCenterTime()) < 2,
					"验证数据库inAuthenticateCenterTime跟系统当前时间差为2分钟内");
			
			Logger.comment("验证delivery");
			
			DeliveryPo actual = orderWapper
					.selectDeliverysByOrderId(orderId)
					.stream()
					.filter(deliveryPo -> deliveryPo.getBillNo()
							.equalsIgnoreCase(billNo)).collect(Collectors.toList()).get(0);
			Logger.verifyEquals(true, actual.getInAuthenticateCenter(),
					"验证inAuthenticateCenter");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AuthenticateCenterNotify_002() {
		Logger.start(true, "批量到达鉴别中心成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			TradingServiceResp placeOrder = TradingPlaceAuthenticateCallService
					.placeOrderWithAuthenticateWithTwoProduct(
							AuthenticateFreightTypeEnum.BUYER, BigDecimal.ONE,
							AuthenticateFeeTypeEnum.BUYER, BigDecimal.TEN);
			
			Order order = placeOrder.getOrderList().get(0);
			
			Long userId = placeOrder.getUserId();
			Long sellerId = placeOrder.getOrderList().get(0).getSellerId();
			Long orderId = placeOrder.getOrderList().get(0).getOrderId();
			
			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(), placeOrder.getMainOrderId());
			
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			
			String billNo1 = TradingCallService.delivery(sellerId, orderId, false);
			String billNo2 = TradingCallService.delivery(sellerId, orderId, false);
			
			Logger.comment("前置数据准备结束");

			String catalogId1 = order.getOrderDetailPoList().get(0)
					.getCatalogId();
			String catalogId2 = order.getOrderDetailPoList().get(1)
					.getCatalogId();

			// 准备参数
			List<IdentifyProdArrivedVo> prodArriveds = new ArrayList<>();
			
			IdentifyProdArrivedVo identifyProdArrivedVo1 = new IdentifyProdArrivedVo();
			
			identifyProdArrivedVo1.setCatalogId(catalogId1);
			identifyProdArrivedVo1.setLogisticNo(billNo1);
			identifyProdArrivedVo1.setArrivedNumber(1);
			
			IdentifyProdArrivedVo identifyProdArrivedVo2 = new IdentifyProdArrivedVo();
			
			identifyProdArrivedVo2.setCatalogId(catalogId2);
			identifyProdArrivedVo2.setLogisticNo(billNo2);
			identifyProdArrivedVo2.setArrivedNumber(1);
			
			prodArriveds.add(identifyProdArrivedVo1);
			prodArriveds.add(identifyProdArrivedVo2);
			
			authenticateCenterNotifyReq.setProdArriveds(prodArriveds);
			authenticateCenterNotifyReq.setOrderId(orderId);

			authenticateCenterNotifyCall.setData(authenticateCenterNotifyReq);
			authenticateCenterNotifyCall.callService();
			
			Logger.verifyEquals("ok",
					authenticateCenterNotifyCall.getReturnData(), "验证返回值");

			Logger.comment("验证order_detail");

			for (int i = 0; i < prodArriveds.size(); i++) {

				IdentifyProdArrivedVo identifyProdArrivedVo = prodArriveds
						.get(i);

				OrderDetailPo orderDetailPo = orderShardingWapper
						.selectOrderDetailbyOrderIdAndCatalogId(orderId,
								identifyProdArrivedVo.getCatalogId());

				Logger.verifyEquals(true,
						orderDetailPo.getInAuthenticateCenter(),
						"验证inAuthenticateCenter");
				Logger.verifyEquals(true, Utils.calcTimeSpanMin(orderDetailPo
						.getInAuthenticateCenterTime()) < 2,
						"验证数据库inAuthenticateCenterTime跟系统当前时间差为2分钟内");

				Logger.comment("验证delivery");

				DeliveryPo actual = orderWapper
						.selectDeliverysByOrderId(orderId)
						.stream()
						.filter(deliveryPo -> deliveryPo.getBillNo()
								.equalsIgnoreCase(
										identifyProdArrivedVo.getLogisticNo()))
						.collect(Collectors.toList()).get(0);
				Logger.verifyEquals(true, actual.getInAuthenticateCenter(),
						"验证inAuthenticateCenter");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
